package com.woniuxy.sales.controller;

import com.woniuxy.sales.model.dto.AddressRequest;
import com.woniuxy.sales.model.entity.Address;
import com.woniuxy.sales.model.entity.China;
import com.woniuxy.sales.service.AddressService;
import com.woniuxy.sales.untils.JwtTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
@Api(tags = "收货地址管理")
@Slf4j
public class AddressController {

    @Autowired
    private AddressService addressService;
    @Autowired
    private JwtTemplate jwtTemplate;

    @GetMapping("/getAllProvince")
    @ApiOperation("获取所有省份")
    public ResponseEntity<List<China>> getAllProvince() {
        List<China> list = addressService.getAllProvince();
        log.info("获取所有省份: {}", list);
        return ResponseEntity.ok(list);
    }

    @GetMapping("/getAllCities")
    @ApiOperation("根据省份id，查询城市")
    @ApiImplicitParam(name = "provinceId", value = "省份id", required = true, dataType = "Integer")
    public ResponseEntity<List<China>> getAllCities(Integer provinceId) {
        log.info("省份id：{}", provinceId);
        List<China> list = addressService.getAllCities(provinceId);
        return ResponseEntity.ok(list);
    }

    @GetMapping("/getAllDistricts")
    @ApiOperation("根据城市id，查询区/县")
    @ApiImplicitParam(name = "cityId", value = "城市id", required = true, dataType = "Integer")
    public ResponseEntity<List<China>> getAllDistricts(Integer cityId) {
        log.info("城市id：{}", cityId);
        List<China> list = addressService.getAllDistricts(cityId);
        return ResponseEntity.ok(list);
    }

    @PostMapping("/save")
    @ApiOperation("添加地址")
    public ResponseEntity add(@RequestBody AddressRequest addressRequest, @RequestHeader("Authorization") String token) {
        // 获取当前登录的用户id
        String userId = jwtTemplate.getValue(token, "userId");
        addressService.addAddress(addressRequest, Integer.valueOf(userId));
        return ResponseEntity.ok().build();
    }

    @GetMapping("/findAddress")
    @ApiOperation("查询所有地址")
    public ResponseEntity query(@RequestHeader("Authorization") String token) {
        // 获取当前登录的用户id
        String userId = jwtTemplate.getValue(token, "userId");
        List<Address> addresses = addressService.query(Integer.valueOf(userId));
        return ResponseEntity.ok(addresses);

    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除地址")
    public ResponseEntity remove(@PathVariable Integer id) {
        addressService.remove(id);
        return ResponseEntity.ok().build();
    }
    @PutMapping("/edit")
    @ApiOperation("修改地址")
    public ResponseEntity edit(@RequestBody Address address) {
        addressService.edit(address);
        return ResponseEntity.ok().build();
    }

}